package com.dwbf.ojbackendjudgeservice.judge.codesandbox;

import com.dwbf.ojbackendjudgeservice.judge.codesandbox.impl.ExampleCodeSandBox;
import com.dwbf.ojbackendjudgeservice.judge.codesandbox.impl.RemoteCodeSandBox;
import com.dwbf.ojbackendjudgeservice.judge.codesandbox.impl.ThirdPartyCodeSandBox;
import com.dwbf.ojbackendmodel.enums.CodeSandBoxEnum;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 代码沙箱创建工厂（根据字符串参数创建指定的代码沙箱实例）
 */
@ConfigurationProperties(prefix = "codesandbox")
@Configuration
@Data
public class CodeSandBoxFactory {

    /**
     * 没有值默认是 远程代码沙箱
     */
    private CodeSandBoxEnum type = CodeSandBoxEnum.REMOTE;

    /**
     * 获取一个新的代码沙箱实例，实例为yml配置的实例
     *
     * @return {@link CodeSandBox}
     */
    public CodeSandBox newInstance() {
        return newInstance(type);
    }



    /**
     * 创建代码沙箱实例
     *
     * @param customerType 沙箱类型
     * @return
     */
    public CodeSandBox newInstance(CodeSandBoxEnum customerType) {
        if (customerType == null) {
            customerType = CodeSandBoxEnum.REMOTE;
        }
        switch (customerType) {
            case EXAMPLE:
                return new ExampleCodeSandBox();
            case REMOTE:
                return new RemoteCodeSandBox();
            case THIRD_PARTY:
                return new ThirdPartyCodeSandBox();
            default:
                return new ExampleCodeSandBox();
        }
    }
}
